package com.jcfk.eam.service.mt;

import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.mt.EamMtCalibrateOrderDao;
import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.dto.base.EamSopStandardDTO;
import com.jcfk.eam.domain.dto.mt.EamMtCalibrateDetailDTO;
import com.jcfk.eam.domain.dto.mt.EamMtCalibrateOrderDTO;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.domain.po.mt.EamMtCalibrateDetail;
import com.jcfk.eam.domain.po.mt.EamMtCalibrateOrder;
import com.jcfk.eam.domain.query.mt.EamMtCalibrateOrderQuery;
import com.jcfk.eam.service.base.EamAssetTypeService;
import com.jcfk.eam.service.base.EamSopStandardService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.List;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * 量检具标定工单
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-09-11
 */
@Service
public class EamMtCalibrateOrderService extends BaseService<EamMtCalibrateOrderDao, EamMtCalibrateOrder> {


    @Autowired
    private EamMtCalibrateDetailService eamMtCalibrateDetailService;

    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    @Autowired
    private EamSopStandardService eamSopStandardService;

    /**
     * EamMtCalibrateOrder分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMtCalibrateOrderDTO> queryByPage(DataPage<EamMtCalibrateOrderDTO> page, EamMtCalibrateOrderQuery query) {
        // 分页查询
        query.setAssetName(StringUtils.isEmpty(query.getAssetName()) ? query.getAssetName() : query.getAssetName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMtCalibrateOrderDTO>
     */
    public List<EamMtCalibrateOrderDTO> getList(EamMtCalibrateOrderQuery query) {
        // 查询条件
        QueryWrapper<EamMtCalibrateOrder> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetId()), EamMtCalibrateOrder::getAssetId, query.getAssetId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetCode()), EamMtCalibrateOrder::getAssetCode, query.getAssetCode());
        qw.lambda().like(!StrUtil.isEmpty(query.getAssetName()), EamMtCalibrateOrder::getAssetName, query.getAssetName());
        qw.lambda().eq(!StrUtil.isEmpty(query.getStandardId()), EamMtCalibrateOrder::getStandardId, query.getStandardId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getUsingDepartmentId()), EamMtCalibrateOrder::getUsingDepartmentId, query.getUsingDepartmentId());
        qw.lambda().ge(!StrUtil.isEmpty(query.getCalibrateDateStart()), EamMtCalibrateOrder::getCalibrateDate, query.getCalibrateDateStart());
        qw.lambda().le(!StrUtil.isEmpty(query.getCalibrateDateEnd()), EamMtCalibrateOrder::getCalibrateDate, query.getCalibrateDateEnd());
        // 查询数据
        List<EamMtCalibrateOrder> list = super.list(qw);

        return convertList(list, EamMtCalibrateOrderDTO.class);
    }

    /**
     * 获取根据关联id详情表列表
     *
     * @param query 查询条件
     * @return List<EamMtCalibrateOrderDTO>
     */
    public List<EamMtCalibrateDetailDTO> getDetails(EamMtCalibrateOrderQuery query) {
        // 查询条件
        QueryWrapper<EamMtCalibrateDetail> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getId()), EamMtCalibrateDetail::getOrderId, query.getId());

        // 查询数据
        List<EamMtCalibrateDetail> list = eamMtCalibrateDetailService.list(qw);

        return convertList(list, EamMtCalibrateDetailDTO.class);
    }


    /**
     * 判断EamMtCalibrateOrder是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMtCalibrateOrderDTO dto) {
        QueryWrapper<EamMtCalibrateOrder> queryWrapper = new QueryWrapper<>();
        return RetResult.ok();
    }

    /**
     * 插入量检具标定工单
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMtCalibrateOrderDTO dto) {

        // 保存
        EamMtCalibrateOrder item = new EamMtCalibrateOrder();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        if (item.getEffectivedDate() != null) {
            EamAssetInfo eamAssetInfo = eamAssetInfoService.getById(dto.getAssetId());
            if (eamAssetInfo != null) {
                eamAssetInfo.setNextCheckDate(item.getEffectivedDate());
                eamAssetInfoService.setDefaultValue(eamAssetInfo);
                eamAssetInfoService.updateById(eamAssetInfo);
            }
        }

        // 保存明细表
        eamMtCalibrateDetailService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 更新量检具标定工单
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMtCalibrateOrderDTO dto) {

        // 保存
        EamMtCalibrateOrder item = new EamMtCalibrateOrder();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        if (item.getEffectivedDate() != null) {
            EamAssetInfo eamAssetInfo = eamAssetInfoService.getById(dto.getAssetId());
            if (eamAssetInfo != null) {
                eamAssetInfo.setNextCheckDate(item.getEffectivedDate());
                eamAssetInfoService.setDefaultValue(eamAssetInfo);
                eamAssetInfoService.updateById(eamAssetInfo);
            }
        }
        // 保存明细表
        eamMtCalibrateDetailService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMtCalibrateOrder item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());
    }


    /**
     * 根据主键获取量检具标定工单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMtCalibrateOrderDTO get(String id) {
        // 查询数据
        EamMtCalibrateOrder item = this.baseMapper.selectById(id);
        EamMtCalibrateOrderDTO dto = new EamMtCalibrateOrderDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        EamAssetInfo eamAssetInfo = eamAssetInfoService.getById(dto.getAssetId());
        if (eamAssetInfo != null) {
            dto.setAssetCode(eamAssetInfo.getAssetTypeName());
            dto.setAssetName(eamAssetInfo.getAssetTypeCode());
            dto.setUsingPersonName(eamAssetInfo.getUsingPersonName());
            dto.setUsingDepartmentName(eamAssetInfo.getUsingDepartmentName());
            dto.setUsingDepartmentName(eamAssetInfo.getUsingDepartmentName());
            dto.setManufactureCode(eamAssetInfo.getManufactureCode());
            dto.setCheckPeriod(eamAssetInfo.getCheckPeriod());
            dto.setMakerId(eamAssetInfo.getMakerId());
            dto.setMakerName(eamAssetInfo.getMakerName());
            dto.setMakerCode(eamAssetInfo.getMakerCode());
            dto.setMeasuringDivision(eamAssetInfo.getMeasuringDivision());
            dto.setMeasuringRange(eamAssetInfo.getMeasuringRange());
        }
        EamAssetTypeDTO assetType = eamAssetTypeService.get(dto.getAssetTypeId());
        if (assetType != null) {
            dto.setAssetTypeName(assetType.getAssetTypeName());
            dto.setAssetTypeCode(assetType.getAssetTypeCode());
        }
        QueryWrapper<EamMtCalibrateDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamMtCalibrateDetail::getOrderId, id);
        List<EamMtCalibrateDetail> list = eamMtCalibrateDetailService.list(queryWrapper);
        dto.setDetails(convertList(list, EamMtCalibrateDetailDTO.class));

        EamSopStandardDTO eamSopStandardDTO = eamSopStandardService.get(item.getStandardId());
        if (eamSopStandardDTO != null) {
            dto.setStandardCode(eamSopStandardDTO.getStandardCode());
            dto.setStandardName(eamSopStandardDTO.getStandardName());
        }
        return dto;
    }


    /**
     * 根据主键获取量检具标定工单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        this.removeById(id);
    }
}

